package cn.ex.ymcc.service.impl;

import cn.ex.ymcc.domain.CourseMarket;
import cn.ex.ymcc.domain.CourseUserLearn;
import cn.ex.ymcc.dto.PayNotifyDto;
import cn.ex.ymcc.mapper.CourseUserLearnMapper;
import cn.ex.ymcc.service.ICourseMarketService;
import cn.ex.ymcc.service.ICourseUserLearnService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aze
 * @since 2022-07-25
 */
@Service
public class CourseUserLearnServiceImpl extends ServiceImpl<CourseUserLearnMapper, CourseUserLearn> implements ICourseUserLearnService {

    @Autowired
    private ICourseMarketService courseMarketService;
    /**
     * 添加用户购买成功记录
     * @param dto
     */
    @Override
    public void saveUserLear(PayNotifyDto dto) {
        Map<String,Object> map = JSONObject.parseObject(dto.getExtParams(), Map.class);
        Integer userId = (Integer) map.get("userId");
        String[] courseIds = map.get("courseId").toString().split(",");
        for (String courseId : courseIds) {
            CourseMarket market = courseMarketService.selectById(courseId);
            //查询曾今是否已经有记录
            CourseUserLearn learnTmp = selectByCourseIdAndLoginId(Long.valueOf(courseId), Long.valueOf(userId));
            Date now = new Date();
            if(learnTmp == null){
                //新增
                CourseUserLearn learn = new CourseUserLearn();
                learn.setLoginId(Long.valueOf(userId));
                learn.setStartTime(now);
                Date endTime = DateUtils.addDays(now, market.getValidDays());
                learn.setEndTime(endTime);
                learn.setState(CourseUserLearn.STATE_PAY);
                learn.setCourseId(Long.valueOf(courseId));
                learn.setCourseOrderNo(dto.getOrderNo());
                learn.setCreateTime(now);
                insert(learn);
            }else{
                learnTmp.setCourseOrderNo(dto.getOrderNo());
                //看结束时间是否小于当前时间
                if(learnTmp.getEndTime().before(now)){//过期了
                    //课程时间过期，直接覆盖修改
                    learnTmp.setLoginId(Long.valueOf(userId));
                    learnTmp.setStartTime(now);
                    Date endTime = DateUtils.addDays(now, market.getValidDays());
                    learnTmp.setEndTime(endTime);
                    learnTmp.setState(CourseUserLearn.STATE_PAY);
                    learnTmp.setCourseId(Long.valueOf(courseId));
                    learnTmp.setCreateTime(now);
                }else{
                    //课程没有过期，添加天数，修改可看时间
                    Date endTime = DateUtils.addDays(learnTmp.getEndTime(), market.getValidDays());
                    learnTmp.setEndTime(endTime);
                }
                updateById(learnTmp);//更新
            }
        }
    }

    public CourseUserLearn selectByCourseIdAndLoginId(Long courseId, Long loginId){
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("login_id",loginId);
        return selectOne(wrapper);
    }
}
